from django.shortcuts import render,HttpResponse,redirect
from app01 import models

# Create your views here.
def index(request):
    return HttpResponse("欢迎使用")
def info(request):
    return HttpResponse("这是第二个网站")
def user_list(request):
    # 1、去app目录下的templates目录寻找user.html。（需提前配置'DIRS': [])
    # 2、根据app注册顺序逐一去templates目录寻找。
    # 3、如果settings.py 中TEMPLATES中为'DIRS': [BASE_DIR / 'templates'],会优先在项目目录下的templates目录寻找。
    return render(request,"user.html")
def tpl(request):
    name = "刘邦"
    roles=["皇帝","农民","大臣"]
    user_info={"name":"关羽","roles":"荡寇将军","birth":"160年"}
    data_list=[{"name":"关羽","roles":"荡寇将军","birth":"160年"},
               {"name":"张飞","roles":"车骑将军","birth":"168年"},
               {"name":"赵云","roles":"翊军将军","birth":"175年"},
               {"name":"马超","roles":"左将军","birth":"176年"},
               {"name":"黄忠","roles":"后将军","birth":"147年"}
               ]
    return render(request,"tpl.html",{"n1":name,"n2":roles,"n3":user_info,"n4":data_list})

def news(request):
    import requests
    from bs4 import BeautifulSoup
    data_list=[]
    res = requests.get(url="https://www.douban.com/group/topic/315744269/?_i=55437891PVwi6f", headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)AppleWebKit/537.36(KHTML,like Gecko)Chrome/95.0.4638.69 Safari/537.36"})
    res.encoding = 'utf-8'
    soup_obj=BeautifulSoup(res.text,"html.parser")
    comments_object = soup_obj.find(name="ul", attrs={"id": "comments"})
    comment_object = comments_object.find_all(name="li")
    for comment in comment_object:
        ele = comment.find(name="div", attrs={"class": "bg-img-green"})
        name = ele.find(name="a").text
        diqu = ele.find(name="span").text[20:]
        reply = comment.find(name="div", attrs={"class": "markdown"}).text
        data = f"{name}({diqu}):{reply}"
        # print(data)
        data_list.append(data)
    print(data_list)

    return render(request,"news.html")#{"news_data":data_list}
def login(request):
    if request.method=="GET":
        return render(request,"login.html")
    else:
        print(request.POST)
        username=request.POST.get("user")
        password=request.POST.get("密码")
        if username=="鲁亚飞" and password=="123":
            return redirect("https://www.baidu.com/")
        else:
            return render(request,"login.html",{"error_msg":"用户名或密码错误！"})


def orm(request):
    #### 1、新增数据 ####
    # models.Department.objects.create(title="实验室")
    # models.UserInfo.objects.create(name="鲁亚飞",password="123",age=25,size=18,data=99)
    # models.UserInfo.objects.create(name="刘朝朋", password="456", age=27, size=10, data=66)
    models.UserInfo.objects.create(name="范凯", password="985", age=21, size=13, data=1994)
    models.UserInfo.objects.create(name="陈宏伟", password="211", age=30, size=16, data=1988)
    #### 2、删除数据 ####
    # models.UserInfo.objects.filter(id=2).delete()#删除表app01_userInfo中id=2的数据。
    # models.UserInfo.objects.all().delete()     #删除表app01_userInfo中的所有数据。
    #### 3、获取数据 ####
    #data_list数据类型是QuerySet类型,data_list=[对象,对象]。
    # data_list=models.UserInfo.objects.all()
    #     # # print(data_list)
    #     # for obj in data_list:
    #     #     print(obj.id,obj.name,obj.password,obj.age,obj.size,obj.data)

    # data_list=models.UserInfo.objects.filter(id=4)#取id=4的那一行数据，类型是QuerySet类型,需要循环取值
    # # data_list数据类型依然是QuerySet类型,data_list=[对象,]
    # for obj in data_list:
    #     print(obj.id,obj.name,obj.password,obj.age,obj.size,obj.data)
    # row_obj=models.UserInfo.objects.filter(id=5).first()#获取id=4的那一行数据，类型是对象，不需要循环取值
    # print(row_obj.id, row_obj.name, row_obj.password, row_obj.age, row_obj.size, row_obj.data)

    #### 4、更新数据 ####
    # models.UserInfo.objects.all().update(password=789) #将password字段的所有数据并更改为999。
    # models.UserInfo.objects.filter(id=5).update(size=15) #找到id=5的行将对应的size字段更改为15。
    # models.UserInfo.objects.filter(name="鲁亚飞").update(age=18) #找到name="鲁亚飞"的行将对应的age字段更改为18。
    models.UserInfo.objects.filter(name="鲁亚飞").update(data=1990)
    models.UserInfo.objects.filter(name="刘朝朋").update(data=1989)
    return HttpResponse("成功")


def info_list(request):
    # 1.获取数据库中所有的用户信息
    data_list=models.UserInfo.objects.all()
    print(data_list)
    # 2.渲染，返回给用户
    return render(request,"info_list.html",{"data_list":data_list})

def info_add(request):
    if request.method == "GET":
        return render(request,"info_add.html")
    user=request.POST.get("user")
    pwd=request.POST.get("pwd")
    age=request.POST.get("age")
    size=request.POST.get("size")
    color=request.POST.get("color")
    whether=request.POST.get("whether")
    time=request.POST.get("create_time")
    models.UserInfo.objects.create(name=user,password=pwd,age=age,size=size,color=color,whether=whether,create_time=time)
    return redirect("/info/list")


def info_delete(request):
    nid=request.GET.get("nid")
    models.UserInfo.objects.filter(id=nid).delete()
    return redirect("/info/list")